<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="utf-8">
    <title>基本资料</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <style>
        .layui-form-item .layui-input-company {width: auto;padding-right: 10px;line-height: 38px;}
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <div class="layui-form layuimini-form">

            <div class="layui-form-item">
                <label class="layui-form-label">头像</label>
                <div style="text-align: left">
                    <input id="userHeardImp" name="userHeardImp" type="hidden" class="layui-input" th:value="${information.userHeardImp}">
                    <img class="layui-upload layui-anim"
                         data-anim="layui-anim-scaleSpring" id="id_upload_img"
                         style="border-radius: 50%; width: 150px;height: 150px; box-shadow: 0 0 0 4px rgb(0 0 0 /10%);"
                         th:if="${information.ShowImg}"
                         th:src="${information.userHeardImp}">
                    <img class="layui-upload layui-anim"
                         data-anim="layui-anim-scaleSpring" id="id_upload_img"
                         th:src="@{/images/logo.png}"
                         style="border-radius: 50%; width: 150px;height: 150px;box-shadow: 0 0 0 4px rgb(0 0 0 /10%);"
                         th:unless="${information.ShowImg}">
                    <div class="layui-word-aux"
                         style="margin-top: 10px;margin-left: 120px"><button type="button" id="uplodImg" class="layui-btn" style="margin-left:10px!important;">上传</button>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">用户名</label>
                <div class="layui-input-block">
                    <input type="text" name="userName" disabled="disabled" th:value="${session.SPRING_SECURITY_CONTEXT.authentication.principal.username}" class="layui-input">
                    <tip>不可修改</tip>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">用户姓名</label>
                <div class="layui-input-block">
                    <input type="text" name="userInformationName" lay-verify="required" lay-reqtext="真实姓名不可为空" placeholder="请输入真实姓名"  th:value="${information.userInformationName}" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">性别</label>
                <div class="layui-input-block">
                    <select id="userInformationSex" name="userInformationSex" lay-verify="required" lay-reqtext="性别不可为空" th:value="${information.userInformationSex}">
                        <option value="">全部</option>
                        <option value="男">男</option>
                        <option value="女">女</option>
                        <option value="保密">保密</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">手机号</label>
                <div class="layui-input-block">
                    <input type="number" name="userInformationPhoneNumber" lay-verify="required" lay-reqtext="手机号不能为空" placeholder="请输入手机号"  th:value="${information.userInformationPhoneNumber}" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-block">
                    <input type="email" name="userInformationEmail"  placeholder="请输入邮箱"  th:value="${user.userEmail}" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script th:src="@{/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js?v=1.0.4}" charset="utf-8"></script>
<script th:inline="javascript">
    layui.use(['form','miniTab','upload'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            upload = layui.upload,
            miniTab = layui.miniTab;

        var sex = [[${information.userInformationSex}]];
        $("#userInformationSex").each(function() {
            // this代表的是<option></option>，对option再进行遍历
            $(this).children("option").each(function() {
                // 判断需要对那个选项进行回显
                if (this.value == sex) {
                    // 进行回显
                    $(this).attr("selected","selected");
                }
            });
            form.render('select');
        })

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var datas = data.field;
            var index = layer.alert("确认提交？", {
                title: '提示'
            }, function () {
                $.ajax({
                    url: '/user/updateUserInformation',
                    type: "POST",
                    data: datas,
                    success: function (data) {
                        if (data.code == 200) {
                            layer.alert(data.message, {
                                title: "提示",
                            }, function () {
                                // 关闭弹出层
                                layer.close(index);
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                                window.parent.location.reload();
                            })
                        } else {
                            layer.alert(data.message, {
                                title: "提示",
                            }, function () {
                                // 关闭弹出层
                                layer.close(index);
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                                window.parent.location.reload();
                            })
                        }
                    }
                });
                return false;
            });
        });

        let imagePath = '';
        //普通图片上传开始
        var uploadInst = upload.render({
            elem: '#uplodImg',
            url: '/user/uploadMongoDb',
            size: 600, //限制文件大小，单位 KB
            before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    // 保存预览显示图片
                    sessionStorage.setItem("img",result);
                });
            },
            //后台返回数据
            done: function (res) {
                if (res.code=== 200) {
                    // 当上传成功才回显图片
                    layer.msg(res.message);
                    imagePath = res.data;
                    //回去预显示图片并回显
                    $('#id_upload_img').attr('src',sessionStorage.getItem("img"));
                    $("#userHeardImp").attr("value",imagePath);
                } else {
                    layer.msg(res.message);
                }
            },


        });
        //普通图片上传结束


    });
</script>
</body>
</html>